package com.starview.platform.data.type;

import com.starview.star.compiler.canonical.Variable;
import com.starview.star.compiler.standard.StandardNames;
import com.starview.star.compiler.util.PrettyPrintDisplay;
import com.starview.star.compiler.util.PrettyPrintable;

@SuppressWarnings("serial")
public class ContractImplementation implements PrettyPrintable
{
  private final String implementedContract;
  private final Variable implementation;
  private final boolean isDefault;

  public ContractImplementation(String implementedContract, Variable implementation, boolean isDefault)
  {
    this.implementedContract = implementedContract;
    this.implementation = implementation;
    this.isDefault = isDefault;
  }

  public String getImplementedContract()
  {
    return implementedContract;
  }

  public Variable getImplementation()
  {
    return implementation;
  }

  public boolean isDefault()
  {
    return isDefault;
  }

  @Override
  public void prettyPrint(PrettyPrintDisplay disp)
  {
    implementation.prettyPrint(disp);
    disp.appendWord(StandardNames.IMPLEMENTS);
    IType implType = TypeUtils.unwrap(implementation.getType());
    if (TypeUtils.isOverloadedType(implType)) {
      DisplayType.displayContract(disp, TypeUtils.getOverloadedType(implType));
      IType[] requirements = TypeUtils.getOverloadRequirements(implType);
      String sep = StandardNames.WHERE;
      for (IType req : requirements) {
        disp.appendWord(sep);
        sep = StandardNames.ALSO;
        DisplayType.displayContract(disp, req);
      }
    } else
      DisplayType.displayContract(disp, implType);
    if (isDefault)
      disp.appendWord(StandardNames.DEFAULT);
  }

  @Override
  public String toString()
  {
    return PrettyPrintDisplay.toString(this);
  }
}
